package me.send.produce.data.controller;

import java.io.OutputStream;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.LinkedHashMap;
import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import me.send.business.base.Pager;
import me.send.common.poi.ExcelUtil;
import me.send.common.poi.ExcelUtil.ExcelExportData;
import me.send.data.service.pojo.ActivityFundPool;
import me.send.data.service.pojo.ActivityFundPoolLog;
import me.send.data.service.pojo.base.DatatablesViewPage;
import me.send.data.service.service.ActivityService;
import me.send.data.service.service.TreeService;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

@Controller
@RequestMapping(value = "activity")
public class ActivityController {

	@Autowired
	private ActivityService activityService;

	@Autowired
	private TreeService treeService;

	@RequestMapping(value = "fundLog/excel/export")    
    public void exportExcel(HttpServletRequest request, HttpServletResponse response)     
    throws Exception { 
		SimpleDateFormat sdf=new SimpleDateFormat("yyyy/MM/dd HH:mm");
        SimpleDateFormat format=new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        String start_time = request.getParameter("start_time"); 
        String end_time = request.getParameter("end_time");  
        String fundCode = request.getParameter("fundCode");  
        String activityType = request.getParameter("activityType");  
        Integer paramActivityType=null;
        if(fundCode==null||(fundCode!=null&&"".equals(fundCode.trim()))){
        	fundCode=null;
        }
        if(activityType==null||(activityType!=null&&"".equals(activityType.trim()))){
        	activityType=null;
        }else{
        	paramActivityType=Integer.parseInt(activityType);
        }
        if(start_time!=null&&!"".equals(start_time.trim())&&end_time!=null&&!"".equals(end_time.trim())){
        	Date startDate=null;
        	Date endDate=null;
        	try {
        		startDate=sdf.parse(start_time);
        		start_time=format.format(startDate);
        		endDate=sdf.parse(end_time);
        		end_time=format.format(endDate);
        	} catch (ParseException e) {
        		// TODO Auto-generated catch block
        		e.printStackTrace();
        	}
        }else{
        	start_time=null;
        	end_time=null;
        }
		Integer count=activityService.count(fundCode,paramActivityType,start_time,end_time);
        Pager page=new Pager();
        page.setRows(count);
        Integer p=(0/count)+1;
        page.setPage(p);
		
        List<ActivityFundPoolLog> list=activityService.list(fundCode,paramActivityType,start_time,end_time,page); 
		List<String[]> columNames = new ArrayList<String[]>();
        columNames.add(new String[] { "资金池编码", "活动描述", "活动类型（0：商品折扣，1：首次充值，2：优惠券）", "金额", "支出时间", "呼单ID" });

        List<String[]> fieldNames = new ArrayList<String[]>();
        fieldNames.add(new String[] { "fundCode", "activityDesc", "activityType", "amount", "createTime", "orderId" });

        LinkedHashMap<String, List<?>> map = new LinkedHashMap<String, List<?>>();
        map.put("资金池支出", list);
        
        
        ExcelExportData setInfo = new ExcelExportData();
        setInfo.setDataMap(map);
        setInfo.setFieldNames(fieldNames);
        setInfo.setTitles(new String[] { "资金池支出报表"});
        setInfo.setColumnNames(columNames);
	 
	 
        response.setContentType("application/vnd.ms-excel");    
        response.setHeader("Content-disposition", "attachment;filename=fund_pool_log.xls");    
        OutputStream ouputStream = response.getOutputStream();    
        ouputStream.write(ExcelUtil.export2ByteArray(setInfo));
        ouputStream.flush();    
        ouputStream.close();
	}
	
	
	// 查询提现订单列表
	@RequestMapping(value = "fund/pool/log/list")
	public ModelAndView activity_fund_pool_log_list(HttpServletRequest request) {
		ModelAndView modelAndView = new ModelAndView(
				"stage/activity/activity_fund_pool_log");
		modelAndView.addObject("LoadTree",
				treeService.main_tree(0, request, "fund/pool/log/list"));
		modelAndView.addObject("module", "资金池消费记录列表");
		return modelAndView;
	}
	
	// 查询提现订单列表
	@RequestMapping(value = "fund/pool/list")
	public ModelAndView activity_fund_pool_list(HttpServletRequest request) {
		ModelAndView modelAndView = new ModelAndView(
				"stage/activity/activity_fund_pool");
		modelAndView.addObject("LoadTree",
				treeService.main_tree(0, request, "fund/pool/list"));
		modelAndView.addObject("module", "资金池列表");
		return modelAndView;
	}
	
	

	
	
	@RequestMapping(value="fund/pool/page", method=RequestMethod.GET)  
	@ResponseBody  
	public DatatablesViewPage<ActivityFundPool> activity_fund_pool_page(HttpServletRequest request){  
//获取分页控件的信息  
		String start = request.getParameter("start");  
		String length = request.getParameter("length");  
//获取前台额外传递过来的查询条件  
		SimpleDateFormat sdf=new SimpleDateFormat("yyyy/MM/dd HH:mm");
		SimpleDateFormat format=new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
		String start_time = request.getParameter("start_time"); 
		String end_time = request.getParameter("end_time");  
		String fundCode = request.getParameter("fundCode");  
		String status = request.getParameter("status");  
		Integer paramStatus=null;
		if(fundCode==null||(fundCode!=null&&"".equals(fundCode.trim()))){
			fundCode=null;
		}
		if(status==null||(status!=null&&"".equals(status.trim()))){
			status=null;
		}else{
			paramStatus=Integer.parseInt(status);
		}
		if(start_time!=null&&!"".equals(start_time.trim())&&end_time!=null&&!"".equals(end_time.trim())){
			Date startDate=null;
			Date endDate=null;
			try {
				startDate=sdf.parse(start_time);
				start_time=format.format(startDate);
				endDate=sdf.parse(end_time);
				end_time=format.format(endDate);
			} catch (ParseException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}else{
			start_time=null;
			end_time=null;
		}
		
		Pager page=new Pager();
		Integer row=Integer.parseInt(length);
		page.setRows(row);
		Integer startInt=Integer.parseInt(start);
		Integer p=(startInt/row)+1;
		page.setPage(p);
		
		//随便组织的查询结果  
		List<ActivityFundPool> list=activityService.list_pool(fundCode,paramStatus,start_time,end_time,page); 
		Integer count=activityService.count_pool(fundCode,paramStatus,start_time,end_time);
		
		DatatablesViewPage<ActivityFundPool> view = new DatatablesViewPage<ActivityFundPool>();  
		view.setiTotalDisplayRecords(count);  
		view.setiTotalRecords(count);  
		
		view.setAaData(list);  
		return view;  
	}  
	
	
	@RequestMapping(value="fund/pool/log/page", method=RequestMethod.GET)  
    @ResponseBody  
    public DatatablesViewPage<ActivityFundPoolLog> activity_fund_pool_log_page(HttpServletRequest request){  
//获取分页控件的信息  
        String start = request.getParameter("start");  
        String length = request.getParameter("length");  
//获取前台额外传递过来的查询条件  
        SimpleDateFormat sdf=new SimpleDateFormat("yyyy/MM/dd HH:mm");
        SimpleDateFormat format=new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        String start_time = request.getParameter("start_time"); 
        String end_time = request.getParameter("end_time");  
        String fundCode = request.getParameter("fundCode");  
        String activityType = request.getParameter("activityType");  
        Integer paramActivityType=null;
        if(fundCode==null||(fundCode!=null&&"".equals(fundCode.trim()))){
        	fundCode=null;
        }
        if(activityType==null||(activityType!=null&&"".equals(activityType.trim()))){
        	activityType=null;
        }else{
        	paramActivityType=Integer.parseInt(activityType);
        }
        if(start_time!=null&&!"".equals(start_time.trim())&&end_time!=null&&!"".equals(end_time.trim())){
        	Date startDate=null;
        	Date endDate=null;
        	try {
        		startDate=sdf.parse(start_time);
        		start_time=format.format(startDate);
        		endDate=sdf.parse(end_time);
        		end_time=format.format(endDate);
        	} catch (ParseException e) {
        		// TODO Auto-generated catch block
        		e.printStackTrace();
        	}
        }else{
        	start_time=null;
        	end_time=null;
        }
        
        Pager page=new Pager();
        Integer row=Integer.parseInt(length);
        page.setRows(row);
        Integer startInt=Integer.parseInt(start);
        Integer p=(startInt/row)+1;
        page.setPage(p);
        
                //随便组织的查询结果  
        List<ActivityFundPoolLog> list=activityService.list(fundCode,paramActivityType,start_time,end_time,page); 
        Integer count=activityService.count(fundCode,paramActivityType,start_time,end_time);
          
        DatatablesViewPage<ActivityFundPoolLog> view = new DatatablesViewPage<ActivityFundPoolLog>();  
        view.setiTotalDisplayRecords(count);  
        view.setiTotalRecords(count);  
          
        view.setAaData(list);  
        return view;  
    }  
	
	
	
	
}
